<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Newslist.php 138 2009-05-25 06:42:38Z movoin $
 */

function _ctlNewslist($name, $args=null)
{
    $opts = array('classid', 'limit', 'strlen', 'site');
    $data = FLEA_WebControls::extractAttribs($args, $opts);
    FLEA_WebControls::mergeAttribs($args);

    if(!$data['classid'] || $data['classid']==0)
        $data['classid'] = null;
    if(!$data['site'])
        $data['site'] = 0;
    if(!$data['strlen'])
        $data['strlen'] = 15;
    if(!$data['limit'])
        $data['limit'] = 5;

    //缓存过期时间
    $deploy = defined('DEPLOY_MODE') && DEPLOY_MODE;
    if ( $deploy ) {
        $lifetime = 86400; //一天
    } else {
        $lifetime = 0;
    }
    //缓存名称
    $cache_id = 'index.newslist.' . $name . '.' . $data['site'];
    if($data['classid'])
        $cache_id .= '.' . $data['classid'];
    //从缓存中读取数据
    $content = FLEA::getCache($cache_id, $lifetime, true, true);
    if(!is_array($content)) {
        //载入新闻模型
        $modelNews =& FLEA::getSingleton( 'Model_News' );
        $content = $modelNews->getContents((int)$data['classid'], 'id,title,created', $data['limit']);
        FLEA::writeCache( $cache_id, $content, true );
    }

    if(!function_exists(cutString)) {
        FLEA::loadFile( 'Helper_CutString' );
    }

    if($content) {
        foreach($content as $news) {
            $output .= '<li><span>' . date('Y/m/d', $news['created']) . '</span><a href="' . url('news', 'view', array('newsid'=>$news['id'])) . '">&middot; ' . cutString($news['title'], (int)$data['strlen']) . '</a></li>';
        }
    }
    return $output;

}
